<?php
/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-15
 */
if (!defined('IN_TINPHP')) {
    exit('Access Denied');
}

require_once('indexApp.php');
require_once(TIN_APP_PATH. "include/utils/Crypt3Des.Class.php");
/**
 * 前台用户登录等
 * */
class UserController extends indexAppController {
	
	public function __construct() {
		$this->chkLogin = false;
        parent::__construct();
	}
 	/** 登录 */
 	public function actionDefault(){
 		//T::$tplFile='front/index/login';

/*
 三种数据操作用法
//         $view = new AssignTable('SYS_GEO');
//         $comLogic = new CommonLogic($view);

//			$comLogic = new CommonLogic('SYS_GEO');
//         $uList = $comLogic->getTableAll("*", array("ID"=>"1"));
//         var_dump($uList);
        
        //$uList = $this->Logic('AccountLogic')->getAccountList("*", "1=1");
        //var_dump($uList);
*/ 
 	}
 	
 	public function actionLogin(){
 		T::$tplFile='front/loginbox';
 		T::$tpl->set("login_type","box");
 	}
 	public function actionReg(){ 		
 		T::$tplFile='front/user/reg';
 	}
 	public function actionChk() {
 		T::$tplFile='public/empty';
 		$res = array();
 		$res['code'] = 'error';
 		$res['msg'] = '异常';

 		$clientid = $_GET['clientid'];
 		if($clientid){
 			$chkdata = $_GET[$clientid];
 			$cond = $clientid."='".$chkdata."'";
 			$userLogic = new UserLogic();
 			$uItem = $userLogic->getUserItem('*', $cond);
 			if($uItem){                                
                                // 修改状态
                                if(isset($_GET["id"]) && $_GET["id"] == $uItem["ID"]){
                                        $res['code'] = 'success';
                                        $res['msg'] = '';   
                                }else{
                                        $res['code'] = 'error';
                                        $res['msg'] = '已存在';       
                                }
 			}else{
 				$res['code'] = 'success';
 				$res['msg'] = '';
 			}
 		}
 		echo $this->getResources($res);
 	}
 	public function actionChkLogin() {
 		T::$tplFile='public/empty';
 		if($_POST){
 			if(RequestUtil::getPost('checkCode')=="" ||	trim($_SESSION["validCode"]) != RequestUtil::getPost('checkCode')){
 				return $this->sysMessage("验证码错误。");
 			}else{
	 			$tourl = $_REQUEST['tourl'];
	 			$cond = "state=1 and username='".RequestUtil::getPost('username')."' and password='".md5(RequestUtil::getPost('password'))."'";
	 		
	 			$userLogic = new UserLogic();
	 			$uItem = $userLogic->getUserItem('*', $cond);
	 			if($uItem){
	 				//---获取用户类型
	 				$uItem['userType'] = $userLogic->getUserTypeItem("*", "ID=?", array($uItem['USERTYPE_ID']));
	 				//---更新登录信息---
	 				$upDate = array();
	 				$upDate['LOGIN_LAST_DATE'] = date("Y-m-d H:i:s", time());
	 				$upDate['LOGIN_NUM'] = "LOGIN_NUM+1";
	 				$upDate['LOGIN_FROM_IP'] = T::getClientIP();
	 				$userLogic->updateFieldVal($upDate, "ID=".$uItem['ID']);
	 				//---创建session登录
	 				$uItem['LOGIN_TIME'] = date("Y-m-d H:i:s", time());
	 				Auth::setUserInfo($uItem);
	 				if(!$tourl) $tourl = 'user.php';

	 				/* 龙付通同步登录 */
	 				$passWord = RequestUtil::getPost('password');
	 				$sid = date('YmdHis')."/".$uItem['USERNAME']."/".$passWord."/".$uItem['ID']; 
		            $crypt = new Crypt3Des();
		            $sid = URLEncode($crypt->encrypt($sid));
		            $login_html  = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
            		$login_html .= '<script type="text/javascript" src="'.TIN_USER_LOGIN_SSO_URL.''.$sid.'"></script>';
            		$login_html .= '<script type="text/javascript">setTimeout("window.location=\''.$tourl.'\'",500);</script>';
		            $login_html .= '<title>登录中</title></head><body style="line-height:30px">正在验证登录。请稍后。。。<br/>如果您的浏览器无法自动转向，请<a href="'.$gotobackurl.'">点击这里</a></body></html>';
		            echo $login_html;
	 			}else{
	 				return $this->sysMessage("用户名或密码错误");
	 			}
 			}
 		}
 	}

 	public function actionReg1() {

 		if(!$_SESSION['regdata']){
 			$user_data = $_POST;
	 		if($user_data){
				//验证码
			/*	if(RequestUtil::getPost('checkCode')=="" ||	trim($_SESSION["validCode"]) != RequestUtil::getPost('checkCode')){
		 			$res['msg'] = '验证码错误。';
		 		}else{*/	
		 			$userLogic = new UserLogic();
					foreach($user_data as $k=>$v)
					{
						$user_data[$k] = htmlspecialchars(addslashes($v));
					}

					if(!preg_match('/^(?:13\d{9}|15[0|1|2|3|5|6|7|8|9]\d{8}|18[0|2|3|5|6|7|8|9]\d{8}|14[5|7]\d{8})$/',$user_data['username'])){
						return $this->sysMessage("手机号格式不对");
					} 
					$cond = "USERNAME ='".$user_data['username']."'";
					$uItem = $userLogic->getUserItem('*', $cond);
					if($uItem){
						return $this->sysMessage("手机号格式已注册过!");
					}

					if(trim($user_data['password'])!=trim($user_data['password_confirm']))
					{
						return $this->sysMessage("两次密码不一样");
						//$res['msg'] = '两次密码不一样!';
					}
					if(trim($user_data['password'])=='')
					{
						return $this->sysMessage("密码不能为空");
						//$res['msg'] = '密码不能为空!';
					}
					$_SESSION['regdata'] = $user_data;
		 			//$uItem = $userLogic->addUserItem($user_data);
				//}
				//print_r($res);
			}else{
				return $this->sysMessage("信息不完整");
			}
		}
		T::$tpl->set("user_data", $_SESSION['regdata']);
	}
 	public function actionReg2() {
 		//T::$tplFile='public/empty';
 		$user_data = $_SESSION['regdata'];
		if($user_data){
			$mobile_verify = $_POST['id_code'];
		 	$smsLogic = new SmsLogic();
		 	$mobile = $user_data['username'];
			if($mobile){
				$posttime = date("Y-m-d",time());
				$where = "MOBILE='".$mobile."' AND POSTTIME='".$posttime."'";
				$r = $smsLogic->getSms('*', $where);
				if($r && $r['ID_CODE']==$mobile_verify){

					$userLogic = new UserLogic();
					$user_data['password'] =  md5($user_data['password']);
					$uid = $userLogic->addUserItem($user_data);
					
					$user_data['userid'] = $uid;
					$infoid = $userLogic->addUserInfo($user_data);
		
					$dataArr['INFOID'] = $infoid;
					$userLogic->updateFieldVal($dataArr, "ID=".$uid);

					if($uid && $infoid){
						unset($_SESSION['regdata']);

						$uItem = $userLogic->getUserItem('*', "ID=".$uid);
						$uItem['userType'] = $userLogic->getUserTypeItem("*", "ID=?", array($uItem['USERTYPE_ID']));
						$uItem['LOGIN_TIME'] = date("Y-m-d H:i:s", time());
						Auth::setUserInfo($uItem);
					}else{
						return $this->sysMessage("注册失败.",array("返回首页"=>"index.php"), 3);
					}
					
				}else{
					return $this->sysMessage("验证码错误。");
				}
			}
		}else{
			return $this->sysMessage("抱歉,因为长时间没处理,数据已超时,请重新注册.",array("返回首页"=>"index.php"), 3);
		}
	}
 	public function actionsms() {
 		T::$tplFile='public/empty';
 		$user_data = $_SESSION['regdata'];
		if($user_data){
			$mobile = $user_data['username'];
			if(!preg_match('/^(?:13\d{9}|15[0|1|2|3|5|6|7|8|9]\d{8}|18[0|2|3|5|6|7|8|9]\d{8}|14[5|7]\d{8})$/',$mobile)) exit('mobile phone error');
			$id_code = $this->random(6);//唯一吗，用于扩展验证
			$send_txt = $id_code;
			$result = file_get_contents('http://service.winic.org/sys_port/gateway/?id=fancaineng2433&pwd=fan48331310&to='.$mobile.'&content='.iconv('UTF-8', 'GB2312', $send_txt).'&time=');
			$resultarr =explode('/',$result);
			if ($resultarr[0]==000){
				$smsLogic = new SmsLogic();
				$smsdata['MOBILE'] = $mobile;
				$smsdata['ID_CODE'] = $id_code;
				$smsdata['MSG'] = $send_txt;
				$uItem = $smsLogic->addSms($smsdata);
				echo 0;
			}else{
				echo $result;
			}
		}else{
			return $this->sysMessage("抱歉,因为长时间没处理,数据已超时,请重新注册.",array("返回首页"=>"index.php"), 3);
		}
	}
 	public function actionSmsVerify() {
 		T::$tplFile='public/empty';
 		$user_data = $_SESSION['regdata'];
		if($user_data){
			$mobile_verify = $_GET['id_code'];
		 	$smsLogic = new SmsLogic();
		 	$mobile = $user_data['username'];
			if($mobile){
				$posttime = date("Y-m-d",time());
				$where = "MOBILE='".$mobile."' AND POSTTIME='".$posttime."'";
				$r = $smsLogic->getSms('*', $where);
				if($r && $r['ID_CODE']==$mobile_verify){
					exit("1");
				}else{
					exit("0");
				}
			}
		}else{
			exit("0");
		}
	}

 	/** 退出登录 */
 	public function actionLogout(){
 		T::$tplFile='public/empty';
 		Auth::loginOutUser();
 		echo '<script language="javascript" src="'.TIN_USER_LOGIN_SIGNOUT_URL.'"></script>';
        echo '<script language="javascript">window.location="index.php";</script>';

 		//return $this->sysMessage("已经安全退出",array("返回首页"=>"index.php"), 3);
 	}
}//End CLass
?>
